package com.tgy.leetcode;

import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @Author: tgy
 * @Date: 12/28/20 9:58 AM
 *  给定由开始时间和结束时间组成的会议时间间隔数组，请返回至少要几间会议室。
 *
 *  如:
 *  [[0, 30],[5, 10],[15, 20]],
 *  返回 2
 */
public class _252_会议室_I {

    public int minMeetingRooms(int[][] meetingRooms) {

        if (meetingRooms == null || meetingRooms.length == 0 || meetingRooms[0].length == 0) {

            return 0;
        }

        Arrays.sort(meetingRooms,((o1, o2) -> {

            return o1[0] - o2[0];
        }));

        PriorityQueue<Integer> minHeap = new PriorityQueue<>();

        for (int i = 0; i < meetingRooms.length; i++) {

            int min = 0;
            if (!minHeap.isEmpty() &&  (min = minHeap.peek()) < meetingRooms[i][0]) {

                minHeap.poll();
            }
            minHeap.add(meetingRooms[i][1]);
        }

        return minHeap.size();
    }
}
